<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="公寓ID" prop="apartmentId">
        <el-input
          v-model="queryParams.apartmentId"
          placeholder="请输入公寓ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="楼号" prop="buildingNumber">
        <el-input
          v-model="queryParams.buildingNumber"
          placeholder="请输入楼号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单元号" prop="unitNumber">
        <el-input
          v-model="queryParams.unitNumber"
          placeholder="请输入单元号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="楼层" prop="floor">
        <el-input
          v-model="queryParams.floor"
          placeholder="请输入楼层"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="面积" prop="squareFeet">
        <el-input
          v-model="queryParams.squareFeet"
          placeholder="请输入面积"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="卧室数量" prop="bedrooms">
        <el-input
          v-model="queryParams.bedrooms"
          placeholder="请输入卧室数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="浴室数量" prop="bathrooms">
        <el-input
          v-model="queryParams.bathrooms"
          placeholder="请输入浴室数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="租金金额" prop="rentAmount">
        <el-input
          v-model="queryParams.rentAmount"
          placeholder="请输入租金金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否可用" prop="available">
        <el-input
          v-model="queryParams.available"
          placeholder="请输入是否可用"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="租户ID" prop="tenantId">
        <el-input
          v-model="queryParams.tenantId"
          placeholder="请输入租户ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="租户名字" prop="tenantFirstName">
        <el-input
          v-model="queryParams.tenantFirstName"
          placeholder="请输入租户名字"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="租户姓氏" prop="tenantLastName">
        <el-input
          v-model="queryParams.tenantLastName"
          placeholder="请输入租户姓氏"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="租户电子邮件" prop="tenantEmail">
        <el-input
          v-model="queryParams.tenantEmail"
          placeholder="请输入租户电子邮件"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="租户电话号码" prop="tenantPhone">
        <el-input
          v-model="queryParams.tenantPhone"
          placeholder="请输入租户电话号码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="租户出生日期" prop="tenantDob">
        <el-date-picker clearable
          v-model="queryParams.tenantDob"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择租户出生日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="租户紧急联系人姓名" prop="tenantEmergencyContactName">
        <el-input
          v-model="queryParams.tenantEmergencyContactName"
          placeholder="请输入租户紧急联系人姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="租户紧急联系人电话" prop="tenantEmergencyContactPhone">
        <el-input
          v-model="queryParams.tenantEmergencyContactPhone"
          placeholder="请输入租户紧急联系人电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="租赁开始日期" prop="leaseStartDate">
        <el-date-picker clearable
          v-model="queryParams.leaseStartDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择租赁开始日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="租赁结束日期" prop="leaseEndDate">
        <el-date-picker clearable
          v-model="queryParams.leaseEndDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择租赁结束日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="租赁金额" prop="leaseAmount">
        <el-input
          v-model="queryParams.leaseAmount"
          placeholder="请输入租赁金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="支付ID" prop="paymentId">
        <el-input
          v-model="queryParams.paymentId"
          placeholder="请输入支付ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="支付日期" prop="paymentDate">
        <el-date-picker clearable
          v-model="queryParams.paymentDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择支付日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="支付金额" prop="paymentAmount">
        <el-input
          v-model="queryParams.paymentAmount"
          placeholder="请输入支付金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="支付方式" prop="paymentMethod">
        <el-input
          v-model="queryParams.paymentMethod"
          placeholder="请输入支付方式"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="维修请求ID" prop="maintenanceRequestId">
        <el-input
          v-model="queryParams.maintenanceRequestId"
          placeholder="请输入维修请求ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="维修请求日期" prop="maintenanceRequestDate">
        <el-date-picker clearable
          v-model="queryParams.maintenanceRequestDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择维修请求日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="维修完成日期" prop="maintenanceCompletionDate">
        <el-date-picker clearable
          v-model="queryParams.maintenanceCompletionDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择维修完成日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['apartments:apartments:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['apartments:apartments:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['apartments:apartments:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['apartments:apartments:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="apartmentsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="公寓ID" align="center" prop="apartmentId" />
      <el-table-column label="楼号" align="center" prop="buildingNumber" />
      <el-table-column label="单元号" align="center" prop="unitNumber" />
      <el-table-column label="楼层" align="center" prop="floor" />
      <el-table-column label="面积" align="center" prop="squareFeet" />
      <el-table-column label="卧室数量" align="center" prop="bedrooms" />
      <el-table-column label="浴室数量" align="center" prop="bathrooms" />
      <el-table-column label="租金金额" align="center" prop="rentAmount" />
      <el-table-column label="是否可用" align="center" prop="available" />
      <el-table-column label="租户ID" align="center" prop="tenantId" />
      <el-table-column label="租户名字" align="center" prop="tenantFirstName" />
      <el-table-column label="租户姓氏" align="center" prop="tenantLastName" />
      <el-table-column label="租户电子邮件" align="center" prop="tenantEmail" />
      <el-table-column label="租户电话号码" align="center" prop="tenantPhone" />
      <el-table-column label="租户出生日期" align="center" prop="tenantDob" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.tenantDob, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="租户紧急联系人姓名" align="center" prop="tenantEmergencyContactName" />
      <el-table-column label="租户紧急联系人电话" align="center" prop="tenantEmergencyContactPhone" />
      <el-table-column label="租赁开始日期" align="center" prop="leaseStartDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.leaseStartDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="租赁结束日期" align="center" prop="leaseEndDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.leaseEndDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="租赁金额" align="center" prop="leaseAmount" />
      <el-table-column label="支付ID" align="center" prop="paymentId" />
      <el-table-column label="支付日期" align="center" prop="paymentDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.paymentDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="支付金额" align="center" prop="paymentAmount" />
      <el-table-column label="支付方式" align="center" prop="paymentMethod" />
      <el-table-column label="维修请求ID" align="center" prop="maintenanceRequestId" />
      <el-table-column label="维修请求日期" align="center" prop="maintenanceRequestDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.maintenanceRequestDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="维修问题描述" align="center" prop="maintenanceIssueDescription" />
      <el-table-column label="维修状态" align="center" prop="maintenanceStatus" />
      <el-table-column label="维修完成日期" align="center" prop="maintenanceCompletionDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.maintenanceCompletionDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['apartments:apartments:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['apartments:apartments:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改综合公寓管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="楼号" prop="buildingNumber">
          <el-input v-model="form.buildingNumber" placeholder="请输入楼号" />
        </el-form-item>
        <el-form-item label="单元号" prop="unitNumber">
          <el-input v-model="form.unitNumber" placeholder="请输入单元号" />
        </el-form-item>
        <el-form-item label="楼层" prop="floor">
          <el-input v-model="form.floor" placeholder="请输入楼层" />
        </el-form-item>
        <el-form-item label="面积" prop="squareFeet">
          <el-input v-model="form.squareFeet" placeholder="请输入面积" />
        </el-form-item>
        <el-form-item label="卧室数量" prop="bedrooms">
          <el-input v-model="form.bedrooms" placeholder="请输入卧室数量" />
        </el-form-item>
        <el-form-item label="浴室数量" prop="bathrooms">
          <el-input v-model="form.bathrooms" placeholder="请输入浴室数量" />
        </el-form-item>
        <el-form-item label="租金金额" prop="rentAmount">
          <el-input v-model="form.rentAmount" placeholder="请输入租金金额" />
        </el-form-item>
        <el-form-item label="是否可用" prop="available">
          <el-input v-model="form.available" placeholder="请输入是否可用" />
        </el-form-item>
        <el-form-item label="租户ID" prop="tenantId">
          <el-input v-model="form.tenantId" placeholder="请输入租户ID" />
        </el-form-item>
        <el-form-item label="租户名字" prop="tenantFirstName">
          <el-input v-model="form.tenantFirstName" placeholder="请输入租户名字" />
        </el-form-item>
        <el-form-item label="租户姓氏" prop="tenantLastName">
          <el-input v-model="form.tenantLastName" placeholder="请输入租户姓氏" />
        </el-form-item>
        <el-form-item label="租户电子邮件" prop="tenantEmail">
          <el-input v-model="form.tenantEmail" placeholder="请输入租户电子邮件" />
        </el-form-item>
        <el-form-item label="租户电话号码" prop="tenantPhone">
          <el-input v-model="form.tenantPhone" placeholder="请输入租户电话号码" />
        </el-form-item>
        <el-form-item label="租户出生日期" prop="tenantDob">
          <el-date-picker clearable
            v-model="form.tenantDob"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择租户出生日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="租户紧急联系人姓名" prop="tenantEmergencyContactName">
          <el-input v-model="form.tenantEmergencyContactName" placeholder="请输入租户紧急联系人姓名" />
        </el-form-item>
        <el-form-item label="租户紧急联系人电话" prop="tenantEmergencyContactPhone">
          <el-input v-model="form.tenantEmergencyContactPhone" placeholder="请输入租户紧急联系人电话" />
        </el-form-item>
        <el-form-item label="租赁开始日期" prop="leaseStartDate">
          <el-date-picker clearable
            v-model="form.leaseStartDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择租赁开始日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="租赁结束日期" prop="leaseEndDate">
          <el-date-picker clearable
            v-model="form.leaseEndDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择租赁结束日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="租赁金额" prop="leaseAmount">
          <el-input v-model="form.leaseAmount" placeholder="请输入租赁金额" />
        </el-form-item>
        <el-form-item label="支付ID" prop="paymentId">
          <el-input v-model="form.paymentId" placeholder="请输入支付ID" />
        </el-form-item>
        <el-form-item label="支付日期" prop="paymentDate">
          <el-date-picker clearable
            v-model="form.paymentDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择支付日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="支付金额" prop="paymentAmount">
          <el-input v-model="form.paymentAmount" placeholder="请输入支付金额" />
        </el-form-item>
        <el-form-item label="支付方式" prop="paymentMethod">
          <el-input v-model="form.paymentMethod" placeholder="请输入支付方式" />
        </el-form-item>
        <el-form-item label="维修请求ID" prop="maintenanceRequestId">
          <el-input v-model="form.maintenanceRequestId" placeholder="请输入维修请求ID" />
        </el-form-item>
        <el-form-item label="维修请求日期" prop="maintenanceRequestDate">
          <el-date-picker clearable
            v-model="form.maintenanceRequestDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择维修请求日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="维修问题描述" prop="maintenanceIssueDescription">
          <el-input v-model="form.maintenanceIssueDescription" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="维修完成日期" prop="maintenanceCompletionDate">
          <el-date-picker clearable
            v-model="form.maintenanceCompletionDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择维修完成日期">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listApartments, getApartments, delApartments, addApartments, updateApartments } from "@/api/apartments/apartments";

export default {
  name: "Apartments",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 综合公寓管理表格数据
      apartmentsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        apartmentId: null,
        buildingNumber: null,
        unitNumber: null,
        floor: null,
        squareFeet: null,
        bedrooms: null,
        bathrooms: null,
        rentAmount: null,
        available: null,
        tenantId: null,
        tenantFirstName: null,
        tenantLastName: null,
        tenantEmail: null,
        tenantPhone: null,
        tenantDob: null,
        tenantEmergencyContactName: null,
        tenantEmergencyContactPhone: null,
        leaseStartDate: null,
        leaseEndDate: null,
        leaseAmount: null,
        paymentId: null,
        paymentDate: null,
        paymentAmount: null,
        paymentMethod: null,
        maintenanceRequestId: null,
        maintenanceRequestDate: null,
        maintenanceIssueDescription: null,
        maintenanceStatus: null,
        maintenanceCompletionDate: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        buildingNumber: [
          { required: true, message: "楼号不能为空", trigger: "blur" }
        ],
        unitNumber: [
          { required: true, message: "单元号不能为空", trigger: "blur" }
        ],
        floor: [
          { required: true, message: "楼层不能为空", trigger: "blur" }
        ],
        squareFeet: [
          { required: true, message: "面积不能为空", trigger: "blur" }
        ],
        bedrooms: [
          { required: true, message: "卧室数量不能为空", trigger: "blur" }
        ],
        bathrooms: [
          { required: true, message: "浴室数量不能为空", trigger: "blur" }
        ],
        rentAmount: [
          { required: true, message: "租金金额不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询综合公寓管理列表 */
    getList() {
      this.loading = true;
      listApartments(this.queryParams).then(response => {
        this.apartmentsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        apartmentId: null,
        buildingNumber: null,
        unitNumber: null,
        floor: null,
        squareFeet: null,
        bedrooms: null,
        bathrooms: null,
        rentAmount: null,
        available: null,
        tenantId: null,
        tenantFirstName: null,
        tenantLastName: null,
        tenantEmail: null,
        tenantPhone: null,
        tenantDob: null,
        tenantEmergencyContactName: null,
        tenantEmergencyContactPhone: null,
        leaseStartDate: null,
        leaseEndDate: null,
        leaseAmount: null,
        paymentId: null,
        paymentDate: null,
        paymentAmount: null,
        paymentMethod: null,
        maintenanceRequestId: null,
        maintenanceRequestDate: null,
        maintenanceIssueDescription: null,
        maintenanceStatus: null,
        maintenanceCompletionDate: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.apartmentId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加综合公寓管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const apartmentId = row.apartmentId || this.ids
      getApartments(apartmentId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改综合公寓管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.apartmentId != null) {
            updateApartments(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addApartments(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const apartmentIds = row.apartmentId || this.ids;
      this.$modal.confirm('是否确认删除综合公寓管理编号为"' + apartmentIds + '"的数据项？').then(function() {
        return delApartments(apartmentIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('apartments/apartments/export', {
        ...this.queryParams
      }, `apartments_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
